{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example: Static forward free-boundary equilibrium calculations (in OpenSTEP)\n", "\n", "---\n", "\n", "Here we set up the [OpenSTEP](https://github.com/ukaea/OpenSTEP) tokamak, plot its coil and limiter geometry, and solve a static forward free-boundary Grad-Shafranov equilibrium using the packaged coil currents and profile data.\n", "\n", "Refer back to **Example02c** where we analyse convergence of this case using higher resolution spatial meshes - this is required for STEP-like plasmas that have quite complex internal current density profiles!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pickle\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create the machine object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke import build_machine\n", "\n", "tokamak = build_machine.tokamak(\n", " active_coils_path=\"../machine_configs/OpenSTEP/OpenSTEP_simple_active_coils.pickle\",\n", " limiter_path=\"../machine_configs/OpenSTEP/OpenSTEP_simple_limiter.pickle\",\n", " wall_path=\"../machine_configs/OpenSTEP/OpenSTEP_simple_limiter.pickle\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot the machine\n", "fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)\n", "plt.tight_layout()\n", "\n", "tokamak.plot(axis=ax1, show=False)\n", "ax1.fill(tokamak.wall.R, tokamak.wall.Z, color='k', linewidth=1.2, facecolor='w', zorder=0)\n", "\n", "ax1.grid(zorder=0, alpha=0.5)\n", "ax1.set_aspect('equal')\n", "ax1.set_xlabel(r'Major radius, $R$ [m]')\n", "ax1.set_ylabel(r'Height, $Z$ [m]')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Instantiate an equilibrium\n", "\n", "Let's set up the equilibrium and profiles objects using the packaged data. We'll then design an initial plasma flux guess using some approximate information about the plasma shape. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load the p' and FF' profile data from file\n", "with open('../machine_configs/OpenSTEP/OpenSTEP_profiles.pickle', 'rb') as f:\n", " profiles_data = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke import equilibrium_update\n", "from freegsnke.jtor_update import GeneralPprimeFFprime\n", "from freegsnke.equilibrium import PsiGuessParabolicJtor\n", "\n", "# equilibrium\n", "eq = equilibrium_update.Equilibrium(\n", " tokamak=tokamak, # provide tokamak object\n", " Rmin=0.5, Rmax=9.0, # radial range\n", " Zmin=-10.0, Zmax=10.0, # vertical range\n", " nx=65, # number of grid points in the radial direction\n", " ny=129, # number of grid points in the vertical direction\n", ")\n", "\n", "# set active coil currents\n", "currentvec = np.array([\n", " -4790870.39240653,\n", " 5155466.64491291,\n", " 2254704.04332795,\n", " 5458947.31403196,\n", " -7854367.88994826,\n", " 5100000.0,\n", " -2274646.53005716,\n", "])\n", "eq.tokamak.set_all_coil_currents(currentvec)\n", "\n", "\n", "# profiles\n", "profiles = GeneralPprimeFFprime(\n", " eq=eq,\n", " Ip=2.2767e7,\n", " fvac=0.5,\n", " psi_n=np.linspace(0,1,256),\n", " pprime_data=profiles_data['pprime'],\n", " ffprime_data=profiles_data['ffprime']\n", ")\n", "\n", "# create the initial guess using alternate method\n", "parabolic_plasma_psi = PsiGuessParabolicJtor(\n", " Ip = 2.2767e7, # plasma current (to scale the guess)\n", " h = (max(tokamak.wall.R) + min(tokamak.wall.R))/2, # centre of ellipse (R)\n", " k = 0.0, # centre of ellipse (Z)\n", " a = 1.0, # roughly the minor radius of the plasma\n", " b = 1.0*5, # roughly the minor radius times the elongation of expected plasma\n", " psi_scale = 1.0 # might need to vary this default\n", ")\n", "\n", "# set plasma psi guess\n", "_plasma_psi = parabolic_plasma_psi(eq=eq) \n", "eq.plasma_psi = _plasma_psi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load the static nonlinear solver" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke import GSstaticsolver\n", "\n", "GSStaticSolver = GSstaticsolver.NKGSsolver(\n", " eq=eq,\n", " solver_type='LUsparse',\n", " gs_operator_order=4,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The forward solve\n", "\n", "Note that we solve to a high tolerance and the `force_up_down_symmetric` flag has been enabled. This is because of the complexity of the internal plasma current density profile of this equilibrium." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# solver\n", "GSStaticSolver.solve(\n", " eq=eq,\n", " profiles=profiles,\n", " constrain=None,\n", " target_relative_tolerance=1e-10,\n", " force_up_down_symmetric=True,\n", " verbose=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot the converged equilibrium\n", "fig2, ax2 = plt.subplots(1, 1, figsize=(5, 10), dpi=80)\n", "plt.tight_layout()\n", "\n", "ax2.grid(zorder=0, alpha=0.5)\n", "ax2.set_aspect('equal')\n", "eq.tokamak.plot(axis=ax2, show=False)\n", "ax2.fill(tokamak.wall.R, tokamak.wall.Z, color='k', linewidth=1.2, facecolor='w', zorder=0)\n", "eq.plot(axis=ax2, show=False)\n", "ax2.set_xlabel(r'Major radius, $R$ [m]')\n", "ax2.set_ylabel(r'Height, $Z$ [m]')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "eq.tokamak.getCurrents()" ] } ], "metadata": { "kernelspec": { "display_name": "freegsnke", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.20" } }, "nbformat": 4, "nbformat_minor": 4 }